package com.borrowed.book.book.service.impl;

import com.borrowed.book.book.Exception.UnderStockException;
import com.borrowed.book.book.constant.BookConstant;
import com.borrowed.book.book.domain.SBorrowOrder;
import com.borrowed.book.book.domain.SelectBorrowOrderInDto;
import com.borrowed.book.book.domain.SelectBorrowOrderOutDto;
import com.borrowed.book.book.mapper.SBorrowOrderMapper;
import com.borrowed.book.book.service.BorrowOrderService;
import com.borrowed.book.book.service.SBookService;
import com.borrowed.book.system.domain.SysDictPojo;
import com.borrowed.book.system.service.ISysDictService;
import com.borrowed.book.user.domain.SUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BorrowOrderServiceImpl implements BorrowOrderService {
    @Autowired
    private SBorrowOrderMapper borrowOrderMapper;
    @Autowired
    private SBookService bookService;
    @Autowired
    private ISysDictService iSysDictService;
    /**
     * 添加待借订单
     *
     * @param bookId
     */
    @Override
    public void AddBorrowOrderService(Long  bookId) throws UnderStockException,Exception {
        SUser sUser = (SUser) SecurityUtils.getSubject().getPrincipal();

        //借阅前检查书籍库存是否大于0
        bookService.CheckBookRemaining(bookId);
        //检查书架里是否已经存在了
        SBorrowOrder order=checkExist(bookId,sUser.getOpenid(),BookConstant.BORROW_WANT);
        if(order!=null){
            throw new Exception("此书已在书架上了，请勿重复加入。");
        }
        //检查书架只能放置50本书
        List<SysDictPojo> bookNumberDict=iSysDictService.getDictByType("borrowOrderNum");
        if(bookNumberDict!=null&&bookNumberDict.size()>0) {
            Boolean bookNumberFlag = checkBookNumber(sUser.getOpenid(),bookNumberDict.get(0).getKey(),BookConstant.BORROW_WANT);
            if(!bookNumberFlag){
                throw new Exception("书架已满，请删除一些再添加。");
            }
        }


        //保存待借订单数据
        SBorrowOrder borrowOrder=new SBorrowOrder();
        borrowOrder.setBookId(bookId);
        borrowOrder.setCreateTime(new Date());
        borrowOrder.setOpenid(sUser.getOpenid());
        borrowOrder.setBorrowStatus(BookConstant.BORROW_WANT);
        borrowOrderMapper.insert(borrowOrder);
    }

    /**
     * 代借移至收藏书单
     *
     * @param id
     */
    @Override
    public void collectBorrowOrder(Long id) {
        SBorrowOrder borrowOrder=borrowOrderMapper.selectBorrowOrderById(id);
        borrowOrder.setCollectTime(new Date());
        borrowOrder.setBorrowStatus(BookConstant.BORROW_COLLECT);
        borrowOrderMapper.updateBorrowOrder(borrowOrder);
    }

    /**
     * 借阅订单一览查询
     *
     * @param param
     */
    @Override
    public List<SelectBorrowOrderOutDto> selectBorrowOrderList(SelectBorrowOrderInDto param) {
        SUser sysUser = (SUser)SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map=new HashMap<>();
        map.put("borrowStatus",param.getBorrowStatus());
        map.put("openId",sysUser.getOpenid());
        List<SelectBorrowOrderOutDto> data=borrowOrderMapper.selectBorrowOrderList(map);
        return data;
    }

    /**
     * 删除想借书单
     *
     * @param bookId
     */
    @Override
    public void deleteBorrowOrderById(Long bookId) throws Exception {
        if(bookId==null){
            throw new  Exception("删除书籍不能为空");
        }

        SUser sysUser = (SUser)SecurityUtils.getSubject().getPrincipal();

        Map<String,Object> map=new HashMap<>();
        map.put("bookId",bookId);
        map.put("openid",sysUser.getOpenid());
        borrowOrderMapper.deleteBorrorOrderById(map);
    }

    /**
     * 批量删除
     *
     * @param bookIds
     */
    @Override
    public void deleteBorrowOrderByIds(List<Long> bookIds) throws Exception {
        if(bookIds==null||bookIds.size()==0){
            throw new  Exception("删除书籍不能为空");
        }
        SUser sysUser = (SUser)SecurityUtils.getSubject().getPrincipal();

        Map<String,Object> map=new HashMap<>();
        map.put("bookIds",bookIds);
        map.put("openid",sysUser.getOpenid());
        borrowOrderMapper.deleteBorrorOrderByIds(map);
    }


    /**
     * 检查同一本书禁止两次加入收藏夹
     * @param bookId
     * @param openId
     * @param borrowStatus
     * @return
     */
    public SBorrowOrder checkExist(Long bookId,String openId,String borrowStatus){
        Map<String,Object> map=new HashMap<>();
        map.put("bookId",bookId);
        map.put("openId",openId);
        map.put("borrowStatus",borrowStatus);
        SBorrowOrder borrowOrder=borrowOrderMapper.selectBorrowOrderByParam(map);
        return borrowOrder;
    }

    /**
     * 校验书架上最多添加一定数量的图书
     * @param openId
     * @param bookNumber
     * @return
     */
    public Boolean checkBookNumber(String openId,String bookNumber,String borrowStatus){
        Map<String,Object> map=new HashMap<>();
        map.put("openId",openId);
        map.put("borrowStatus",borrowStatus);
        Integer borrowNumbers=borrowOrderMapper.selectBorrowNumber(map);
        if(borrowNumbers!=null&&borrowNumbers>=Integer.valueOf(bookNumber)){
            return false;
        }else{
            return true;
        }
    }
}
